package renguanyu.app.codebuilder.view;

import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.AnchorPane;
import renguanyu.app.codebuilder.dao.InitDao;
import renguanyu.app.codebuilder.util.JDBCFactory;
import renguanyu.app.codebuilder.view.createcode.CreateCodeView;

public class CodeBuilderView extends AnchorPane{

    MainView mainView;
    TabPane main = new TabPane();
    Tab createCode = new Tab("生成代码");
    Tab diyTemplate = new Tab("定义模板");

    CreateCodeView createCodeView = new CreateCodeView(this);
    DiyTemplateView diyTemplateView = new DiyTemplateView(this);

    public CodeBuilderView(MainView mainView){
        this.mainView = mainView;
        initDB();
        initUI(); 
        initEvent();

    }
    private void initDB() {

        if(JDBCFactory.get().tableIsNotExists("TEMP")){
            InitDao.initTemp();
        }
        if(JDBCFactory.get().tableIsNotExists("TEMP_DATA")){
            InitDao.initTempData();
        }
        ObservableList<ObservableMap<String, String>> list = JDBCFactory.get().query("select * from temp_data");
//        System.out.println(list);
    }
    
    private void initUI() {

        main.setPrefHeight(400.0);
        main.setPrefWidth(600.0);
        main.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
        AnchorPane.setTopAnchor(main,0.0);
        AnchorPane.setRightAnchor(main,0.0);
        AnchorPane.setBottomAnchor(main,0.0);
        AnchorPane.setLeftAnchor(main,0.0);

        createCode.setContent(getCreateCodeView());
        diyTemplate.setContent(getDiyTemplateView());

        main.getTabs().addAll(createCode,diyTemplate);

        this.setPrefHeight(400.0);
        this.setPrefWidth(800.0);
        this.getChildren().addAll(main);
    }

    private void initEvent() {

        createCode.setOnSelectionChanged(event -> {
            if(createCode.isSelected()){
                createCode.setContent(getCreateCodeView());

            }

        });
        diyTemplate.setOnSelectionChanged(event -> {
            if(diyTemplate.isSelected()){
                diyTemplate.setContent(getDiyTemplateView());
            }

        });
    }

    public MainView getMainView() {
        return mainView;
    }

    public CreateCodeView getCreateCodeView() {
        return createCodeView;
    }

    public DiyTemplateView getDiyTemplateView() {
        return diyTemplateView;
    }
}
